

//
//  CollectionViewController.m
//  collectionView
//
//  Created by 小白 on 16/3/9.
//  Copyright © 2016年 小白. All rights reserved.
//

#import "CollectionViewController.h"
#import "CollectionViewCell.h"
#import "CollectionReusableView.h"
@interface CollectionViewController ()
@property (nonatomic,copy)NSArray *arrFlags;
@property (nonatomic,copy) NSArray *arrHead;
@end

@implementation CollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
//    创建新的布局
        UICollectionViewFlowLayout *layOut = [[UICollectionViewFlowLayout alloc]init];
        //单元格的尺寸
        layOut.itemSize = CGSizeMake(100, 100);
        //滑动方向
        layOut.scrollDirection = UICollectionViewScrollDirectionVertical;
        //段头大小
       layOut.headerReferenceSize = CGSizeMake(320, 75);
        layOut.minimumInteritemSpacing = 10.0;
//    layOut.minimumLineSpacing =30;
        self.collectionView.collectionViewLayout = layOut;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.arrHead = @[@"African Flags",@"Asian Flags",@"Australasian Flags",@"European Flags",@"North American Flags",@"South American Flags"];
//    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
//    UICollectionView.UICollectionViewLayout = flow;
    // Register cell classes
//    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    
    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    //
    [self.collectionView registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    //取数据
    NSString *path = [[NSBundle mainBundle]pathForResource:@"flag" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    self.arrFlags = arr;
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return self.arrFlags.count;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSArray *arr = self.arrFlags[section];
    return arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    NSArray *arr = self.arrFlags[indexPath.section];
    NSDictionary *dic = arr[indexPath.row];
    NSString *strimg = dic[@"imageName"];
    NSString *strname = dic[@"name"];
    cell.imgView.image = [UIImage imageNamed:strimg];
    cell.labelName.text = strname;
    
 
    
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    CollectionReusableView *cell = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
    cell.labelHead.text = self.arrHead[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *arr = self.arrFlags[indexPath.section];
    NSDictionary *dic = arr[indexPath.row];
    NSString *strImage = dic[@"imageName"];
    NSString *strLabel = dic[@"name"];
    [self.Delegate addImageAndLabelText:strImage AndLabel:strLabel];
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
